<?php

include_once '../model/Constructora.php';
include_once '../daos/ConstrucDao.php';
include_once '../commons/Constants.php';

class ConstrucController {

    public function __construct() {
        
    }

    public function listar() {

        session_start();

        $msj = "Error consultando las constructoras, por favor intente más tarde";
        $view = "../views/constructoras.php";

        try {
            $dao = new ConstrucDao();

            $listaConstructs = array();
            $listaConstructs = $dao->getConstructoras();

            $msj = "";
            $_SESSION['listaConstructs'] = null;
            if (isset($listaConstructs) && count($listaConstructs) > 0) {
                $_SESSION['listaConstructs'] = serialize($listaConstructs);
            }
            
            unset($_SESSION['construc']);
            unset($_SESSION['obra']);
        } catch (Exception $e) {
            
        }

        $_SESSION['msj'] = $msj;
        return $view;
    }

    public function save() {

        session_start();

        $msj = "Error salvando la constructora";
        $estatus = AJAX_ERROR;
        $id = 0;

        try {
            $dao = new ConstrucDao();

            $construct = new Constructora();
            $construct->setId($_POST['id']);
            $construct->setRif(strtoupper($_POST['rif']));
            $construct->setNombre(strtoupper($_POST['nombre']));
            $construct->setDireccion(strtoupper($_POST['direccion']));

            if ($construct->getId() == 0) {

                $construct->setFechaIngreso(date('Y-m-d'));
                if ($dao->getConstrucByRIF($construct->getRif()) != null) {
                    $msj = "La constructora ya existe, por favor verifique";
                    $construct = null;
                } else {

                    $time = strtotime($_POST['fecha']);
                    $construct->setFechaIngreso(date('Y-m-d', $time));

                    $construct = $dao->saveConstructora($construct);
                }
            } else {
                $construct = $dao->updateConstruc($construct);
            }

            if (isset($construct) && $construct->getId() > 0) {
                $msj = "Constructora salvada satisfactoriamente";
                $estatus = AJAX_SUCCESSFUL;
                $id = $construct->getId();
            }
        } catch (Exception $e) {
            
        }

        $responce = array('msj' => $msj, 'estatus' => $estatus, 'id' => $id);

        return json_encode($responce);
    }

    public function query() {

        session_start();

        $responce = array('msj' => "Error consultando la constructora", 'estatus' => AJAX_ERROR);

        try {
            $dao = new ConstrucDao();

            $construct = new Constructora();
            $construct = $dao->getConstrucById($_POST['id']);

            if (isset($construct)) {

                $time = strtotime($construct->getFechaIngreso());

                $responce = array('msj' => "",
                    'estatus' => AJAX_SUCCESSFUL,
                    'id' => $construct->getId(),
                    'rif' => $construct->getRif(),
                    'nombre' => $construct->getNombre(),
                    'direccion' => $construct->getDireccion(),
                    'fecha' => date('d/m/Y', $time));
            }
        } catch (Exception $e) {
            
        }

        return json_encode($responce);
    }

    public function delete() {

        session_start();

        $msj = "Error eliminando la constructora";
        $estatus = AJAX_ERROR;

        try {
            $dao = new ConstrucDao();

            $construct = $dao->getConstrucById($_POST['id']);

            if (isset($construct)) {
                $result = $dao->deleteConstruc($construct);

                if ($result) {
                    $msj = "Constructora eliminada satisfactoriamente";
                    $estatus = AJAX_SUCCESSFUL;
                }
            }
        } catch (Exception $e) {
            
        }

        $responce = array('msj' => $msj, 'estatus' => $estatus);

        return json_encode($responce);
    }
}

?>